﻿@page
@using SXFramWork.Web
@{
    ViewData["Title"] = "任务分组";
    Layout = AdminLayout.Pjax(HttpContext);
}
    <div id="container">
        <div class="list-wall">
            <div class="layui-form list-search">

                <button type="button" class="layui-btn layui-btn-sm layui-hide" data-type="reload">
                    <i class="layui-icon layui-icon-refresh"></i> 刷新
                </button>

                <button type="button" class="layui-btn layui-btn-normal" data-type="Add">
                    <i class="layui-icon layui-icon-add-circle"></i> 添加
                </button>


            </div>
            <table class="layui-hide" id="tablelist" lay-filter="tool"></table>
        </div>

        <script>
            layui.use(['table', 'layer', 'jquery', 'tree', 'common'],
                function () {
                    var table = layui.table,
                        layer = layui.layer,
                        $ = layui.jquery,
                        os = layui.common

                    //加载表格数据
                    os.tableRender({
                        elem: '#tablelist',
                        url: "/api/admin/sysjob/list",
                        cols: [
                            [
                                //{ type: 'checkbox', fixed: 'left' },
                                { field: 'group', title: '分组',width:100 },
                                { field: 'name', title: '名称' },
                                {
                                        title: '触发器类型', width: 100, templet:
                                        function (a) {
                                            if (a.triggerType == 1) return '<span class="layui-badge">CRON</span>';
                                            else return '<span class="layui-badge layui-bg-green">Simple</span>';
                                        }
                                },
                                { field: 'cron', title: 'Cron表达式' },
                                { field: 'interval', title: '时间间隔(s)', width: 100, templet: function (a) { if (a.triggerType == 1) return '--'; else return a.interval } },
                                { field: 'repeatCount', title: '重复次数', width: 100, templet: function (a) { if (a.triggerType == 1) return '--'; else return a.repeatCount } },
                                {   title: '有效期', templet: function (d) {
                                    return '<span>' + d.startTime + '～' + d.endTime + '</span>';
                                }
                                },
                                {
                                    title: '状态',width:90,
                                    templet: function (a) {
                                        if (a.status == 0) return '<span class="layui-badge layui-bg-gray">停止</span>';
                                        else if (a.status == 1) return '<span class="layui-badge layui-bg-orange">运行中</span>';
                                        else if (a.status == 2) return '<span class="layui-badge layui-bg-green">暂停</span>';
                                        else if (a.status == 3) return '<span class="layui-badge">已完成</span>';
                                        else return '<span class="layui-badge">异常</span>'
                                    }
                                },
                                { title: '操作', width: 300, templet: OnLoadTool }
                            ]
                        ]
                      })

                    var active = {

                        reload: function () {
                            os.tableReload('tablelist',
                                { where: {} }
                            )
                        },

                        Add: function () {
                            os.Open('添加任务', '/admin/sysjob/add', '900px', '400px', function () {
                            });
                        }
                       
                    };

                    //头部工具按钮
                    $('.list-search .layui-btn').on('click', function () {
                        var type = $(this).data('type');
                        active[type] ? active[type].call(this) : '';
                    });



                    //动态表格行内按钮
                    function OnLoadTool(data) {
                        let htmls = '';
                        if (data.status ==2) {
                            htmls += '<a class="layui-btn layui-btn-normal" lay-event="start"> 启动</a>';
                            htmls += '<a class="layui-btn layui-btn-danger" lay-event="stop"> 停止</a>';
                        }
                        if (data.status == 1) {
                            htmls += '<a class="layui-btn layui-btn-normal" lay-event="pause"> 暂停</a>';
                            htmls += '<a class="layui-btn layui-btn-danger" lay-event="stop"> 停止</a>';
                        }
                        if (data.status == 0) {
                            htmls += '<a class="layui-btn layui-btn-normal" lay-event="start"> 启动</a>';
                        }

                        htmls += '<a class="layui-btn layui-btn-warm" lay-event="showlog"> 日志</a>';
                        htmls += '<a class="layui-btn layui-btn-danger" lay-event="delete">删除</a>';
                        return htmls;
                    }




                    //监听工具条
                    table.on('tool(tool)', function (obj) {
                        var data = obj.data;
                        //启动
                        if (obj.event == 'start') {
                            os.ajax('api/admin/sysjob/Resume/', data.id, function (res) {
                                if (res.code === 1) {
                                    active.reload();
                                    os.success('任务启动成功');
                                } else {
                                    os.error(res.msg);
                                }
                            });
                        }
                        //查看日志
                        if (obj.event == 'showlog')
                        {
                            os.Open('任务日志(' + data.name + ')', '/admin/sysjob/tasklog/?id=' + data.id, '880px', '600px', function () {

                            })
                        }
                        //删除
                        if (obj.event == 'delete')
                        {
                            layer.confirm('确定要删除此任务吗？', function (index) {
                                layer.close(index);
                                os.ajax('api/admin/sysjob/delete/', data.id, function (res) {
                                    if (res.code === 1) {
                                        active.reload();
                                    } else {
                                        os.error(res.msg);
                                    }
                                });
                            });

                           
                        }
                        //暂停
                        if (obj.event == 'pause')
                        {
                            os.ajax('api/admin/sysjob/pause', data.id, function (res) {
                                if (res.code === 1) {
                                    active.reload();
                                } else {
                                    os.error(res.msg);
                                }
                            });
                        }
                        if (obj.event == 'stop') {

                            os.ajax('api/admin/sysjob/stop', data.id, function (res) {
                                if (res.code === 1) {
                                    active.reload();
                                } else {
                                    os.error(res.msg);
                                }
                            });
                        }

                    });

                });
        </script>

    </div>